﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyDMS.BusinessLayer;

public partial class UploadDocument : PageBaseClass
{
    UploadedDocumentFactory uDocFactory;

    protected void Page_Load(object sender, EventArgs e)
    {
        DocumentTypeFactory docTypeFactory = new DocumentTypeFactory();
        List<DocumentType> docTypes = docTypeFactory.GetAllBy(DocumentType.DocumentTypeFields.Office_Code, _author.office_Code);

        ddlDocumentTypes.DataSource = docTypes;
        ddlDocumentTypes.DataTextField = DocumentType.DocumentTypeFields.Type_Name.ToString();
        ddlDocumentTypes.DataValueField = DocumentType.DocumentTypeFields.DT_Code.ToString();
        ddlDocumentTypes.DataBind();

        txtRecDate.Text = DateTime.Now.ToString("MMM d, yyyy");
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        UploadedDocument uDoc = new UploadedDocument();
        uDoc.Document_Name = txtDocName.Text;
        uDoc.Document_Desc = txtDocDesc.Text;

        string filePath = Server.MapPath("Upload/UserData/Files/") + fluDocument.FileName;
        fluDocument.SaveAs(filePath);

        uDoc.File_Path = filePath;
        uDoc.Recieved_Date = DateTime.Parse(txtRecDate.Text);
        uDoc.Hard_Location = txtLocation.Text;

        uDoc.Document_Type = int.Parse(ddlDocumentTypes.SelectedValue);
        uDoc.Owner = _author.user_Code;
        uDoc.Office_Code = _author.office_Code;

        uDocFactory = new UploadedDocumentFactory();
        if (uDocFactory.Insert(uDoc))
        {
            List<UserFolder> uFolders = new UserFolderFactory().GetAllBy(UserFolder.UserFolderFields.User_Code, _author.user_Code);
            for (int i = 0; i < uFolders.Count; i++)
            {
                if (uFolders[i].Is_Default)
                {
                    UserDocumentInFolder uDocInFolder = new UserDocumentInFolder();
                    uDocInFolder.Document_Code = uDoc.Document_Code;
                    uDocInFolder.Folder_Code = uFolders[i].Folder_Code;

                    new UserDocumentInFolderFactory().Insert(uDocInFolder);
                    break;
                }
            }
            lblMessage.Text = "Saved Successfully";
        }
        else
        {
            lblMessage.Text = "Error!";
        }
        lblMessage.Visible = true;
    }
}
